package com.lglin.controller.admin;

import com.github.pagehelper.PageInfo;
import com.lglin.bean.product.entity.Category;
import com.lglin.bean.user.entity.User;
import com.lglin.common.utils.reponse.ResultBean;
import com.lglin.common.utils.reponse.ResultEnum;
import com.lglin.service.admin.AdminService;
import com.lglin.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.security.RolesAllowed;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin")
@RolesAllowed("ROLE_ADMIN")
public class AdminCntroller {

    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;

    @GetMapping("auth")
    public ResultBean auth() {
        return ResultBean.success().msg("认证成功");
    }

    @GetMapping("user_list/{pageNum}")
    public ResultBean selecUser(@PathVariable int pageNum) {
        PageInfo pageInfo = adminService.selectUser(pageNum);
        return ResultBean.success().data(pageInfo).msg("获取用户信息成功");
    }

    @PostMapping("user_add")
    public ResultBean register(@Validated @RequestBody User user) {
        if (userService.checkNickname(user.getNickname()) && userService.checkEmail(user.getEmail())) {
            boolean result = adminService.addUser(user);
            if (result) {
                return ResultBean.success().setResultEnum(ResultEnum.Register_Success);
            }
        }
        return ResultBean.fail().setResultEnum(ResultEnum.Register_Fail);
    }

    @GetMapping("user_search")
    public ResultBean searchInUser(@RequestParam String start, @RequestParam String end, @RequestParam String email, @RequestParam int pageNum) {
        PageInfo pageInfo = adminService.searchUser(start, end, email, pageNum);
        return ResultBean.success().data(pageInfo).msg("获取用户信息成功");
    }

    @PostMapping("user_update")
    public ResultBean updaeUser(@RequestBody User user) {
        boolean flag = adminService.updateUserInfo(user);
        if (flag)
            return ResultBean.fail().setResultEnum(ResultEnum.Update_Success);
        return ResultBean.fail().setResultEnum(ResultEnum.Update_Fail);
    }

    @GetMapping("user_delete/{ids}")
    public ResultBean deleteUser(@PathVariable String ids) {
        boolean flag = adminService.deleteUser(ids);
        if (flag)
            return ResultBean.fail().setResultEnum(ResultEnum.Update_Success);
        return ResultBean.fail().setResultEnum(ResultEnum.Update_Fail);
    }

    @GetMapping("order_list/{pageNum}")
    public ResultBean selecOrder(@PathVariable int pageNum) {
        PageInfo pageInfo = adminService.selectOrderList(pageNum);
        return ResultBean.success().data(pageInfo).msg("获取订单信息成功");
    }

    @GetMapping("order_delete/{ides}")
    public ResultBean deleteOrder(@PathVariable String ids) {
        boolean flag = adminService.deleteOrder(ids);
        if (flag)
            return ResultBean.fail().setResultEnum(ResultEnum.Update_Success);
        return ResultBean.fail().setResultEnum(ResultEnum.Update_Fail);
    }

    @GetMapping("order_search")
    public ResultBean searchInOrder(@RequestParam String start, @RequestParam String end, @RequestParam String sid, @RequestParam int pageNum) {
        PageInfo pageInfo = adminService.searchOrder(start, end, sid, pageNum);
        return ResultBean.success().data(pageInfo).msg("获取订单信息成功");
    }

    @PostMapping("product_search")
    public ResultBean searchProduct(@RequestBody String data) {
        PageInfo pageInfo = adminService.searchProduct(data);
        return ResultBean.success().data(pageInfo).msg("获取商品信息成功");
    }

    @PostMapping("add_product")
    public ResultBean addProduct(@RequestBody String data) {
        int result = adminService.addProduct(data);
        if (result > 0) return ResultBean.success().data(result).msg("添加成功");
        return  ResultBean.fail().msg("添加失败");
    }

    @GetMapping("product_category/{pageNum}")
    public ResultBean getCategory(@PathVariable int pageNum) {
        return ResultBean.success().data(adminService.getCategory(pageNum));
    }

    @GetMapping("product_info/{id}")
    public ResultBean productInfo(@PathVariable int id) {
        return ResultBean.success().data(adminService.getProductInfo(id));
    }

    @PostMapping("update_product")
    public ResultBean updateProduct(@RequestBody String data) {
        boolean result = adminService.updateProduct(data);
        if (result) return ResultBean.success().msg("修改成功");
        return ResultBean.fail().msg("修改失败");
    }

    @GetMapping("delete_product/{id}")
    public ResultBean deleteProduct(@PathVariable int id) {
        boolean result = adminService.deleteProduct(id);
        if (result) return ResultBean.success().msg("删除成功");
        return ResultBean.fail().msg("删除失败");
    }

    @GetMapping("delete_category/{id}")
    public ResultBean deleteCategory(@PathVariable int id) {
        boolean result = adminService.deleteCategory(id);
        if (result) return ResultBean.success().msg("删除成功");
        return ResultBean.fail().msg("删除失败");
    }

    @PostMapping("add_category")
    public ResultBean addCategory(@Validated @RequestBody Category category) {
        boolean result = adminService.addCategory(category);
        if (result) return ResultBean.success().msg("添加成功");
        return ResultBean.fail().msg("添加失败");
    }

    @PostMapping("upload")
    public ResultBean upload(@RequestParam("file") MultipartFile[] file, @RequestParam("id") int id) {
        adminService.upload(file, id);
        return ResultBean.success().msg("上传成功");
    }

    @GetMapping("update_order/{id}/{status}")
    public ResultBean updateOrderStatus(@PathVariable int id, @PathVariable int status) {
        boolean result = adminService.updateOrderStatus(id, status);
        if (result) return ResultBean.success().msg("修改成功");
        return ResultBean.success().msg("修改失败");
    }


}
